package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CheckingManage;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CheckingManageMapper;
import cn.wolfcode.crm.query.CheckingManageQueryObject;
import cn.wolfcode.crm.service.ICheckingManageService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class CheckingManageServiceImpl implements ICheckingManageService {

    @Autowired
    private CheckingManageMapper mapper;


    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void checkin(CheckingManage record) {
        Employee currentUser = UserContext.getCurrentUser();
        record.setEmployee(currentUser);
        record.setCheckDate(new Date());
        record.setCheckTime(new Date());
        //日期格式化
        boolean state = getstate();

        record.setCheckState(state);
        System.out.println(record.getCheckDate());
        CheckingManage getstate = mapper.getstate(record);
        if (getstate == null){
            mapper.insert(record);
        }else {
            System.out.println(1/0);
        }
    }

    public boolean getstate(){
        Date date = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
        String t1 = "16:00:00";
        try {
            Date parse = sdf1.parse(t1);
            int compareTo = parse.compareTo(date);
            if (compareTo == 0){
                return true;
            }else if (compareTo>0){
                return true;
            }else if (compareTo<0){
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean getOutState(){
        Date date = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
        String t1 = "22:30:00";
        try {
            Date parse = sdf1.parse(t1);
            int compareTo = parse.compareTo(date);
            if (compareTo == 0){
                return true;
            }else if (compareTo>0){
                return true;
            }else if (compareTo<0){
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }

    public CheckingManage selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<CheckingManage> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(CheckingManage record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(CheckingManageQueryObject qo) {

        int total = mapper.query4Count(qo);
        if(total > 0){
            return new PageResult(total,mapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public Date getDate(){
        Date date = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf1.format(date);
        Date parse = null;
        try {
            parse = sdf1.parse(format);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public Date getSuppliment(String sup){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateFormat.parse(sup);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public void checkout(CheckingManage checkingManage) {
        Date date = new Date();
        boolean outState = getOutState();
        Date date1 = getDate();
        Employee currentUser = UserContext.getCurrentUser();
        checkingManage.setEmployee(UserContext.getCurrentUser());
        //签到日期
        checkingManage.setCheckDate(date1);
        //签退时间
        checkingManage.setCheckoutTime(date);
        //签退状态
        CheckingManage getstate = mapper.getOutState(checkingManage);
        checkingManage.setCheckoutState(outState);
        if (getstate.getCheckoutState() == null) {
            mapper.updateByPrimaryKey(checkingManage);
        }else {
            System.out.println(1/0);
        }
    }

    public void supplement(CheckingManage checkingManage,String sup) {
        Date suppliment = getSuppliment(sup);
        checkingManage.setCheckDate(suppliment);
        checkingManage.setRepairTime(new Date());
        checkingManage.setEmployee(UserContext.getCurrentUser());
        CheckingManage getstate = mapper.getstate(checkingManage);
        if (getstate.getCheckState() == null) {
            mapper.supplement(checkingManage);
        }else {
            System.out.println(1/0);
        }
    }
}
